let people = [
    {
        "name": "Alice",
        "sex": "女",
        "age": 25
    },
    {
        "name": "Bob",
        "sex": "男",
        "age": 30
    },
    {
        "name": "Charlie",
        "sex": "男",
        "age": 18
    },
    {
        "name": "David",
        "sex": "男",
        "age": 30
    },
    {
        "name": "Eve",
        "sex": "女",
        "age": 20
    },
    {
        "name": "Frank",
        "sex": "男",
        "age": 25
    },
    {
        "name": "Grace",
        "sex": "女",
        "age": 30
    },
    {
        "name": "Henry",
        "sex": "男",
        "age": 18
    },
    {
        "name": "Ivy",
        "sex": "女",
        "age": 35
    },
    {
        "name": "Jack",
        "sex": "男",
        "age": 20
    }
]

// 使用传入的方式进行分组
function groupBy(arr, generateKey) {
    if (typeof generateKey == 'string') {
        let propName = generateKey;
        generateKey = (item) => item[propName];
    }
    // 参数归一化
    let result = {};
    for (const item of arr) {
        let key = generateKey(item);
        if (!result[key]) {
            result[key] = [];
        }
        result[key].push(item);
    }
    console.log(result);
}

groupBy(people, 'age')
groupBy(people, 'sex')
groupBy(people, item => item['sex'])
groupBy([22, 23, 2, 24, 2, 323, 3, 222, 2, 2332, 2, 21], item => item%2 == 0 ? `偶数` : `奇数`)